﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using Tesla.Math;
using Tesla.Xna.Util;
using XFG = Microsoft.Xna.Framework.Graphics;
using XNA = Microsoft.Xna.Framework;

namespace Tesla.Xna.Graphics.Implementation {
    /// <summary>
    /// Concrete XNA 4.0 implementation for <see cref="BlendState"/>.
    /// </summary>
    public sealed class XNABlendStateImplementation : BlendStateImplementation {
        private XFG.BlendState _blendState;

        /// <summary>
        /// Gets the actual XNA BlendState object.
        /// </summary>
        internal XFG.BlendState XNABlendstate {
            get {
                return _blendState;
            }
        }


        /// <summary>
        /// Gets the number of render targets that blending can be enabled for.
        /// </summary>
        public override int BlendEnableCount {
            get {
                return 1;
            }
        }


        /// <summary>
        /// Gets the number of render targets that can have an independent write mask for.
        /// </summary>
        public override int ColorWriteChannelsCount {
            get {
                return 4;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="XNABlendStateImplementation"/>.
        /// </summary>
        internal XNABlendStateImplementation() : base() {}

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="XNABlendStateImplementation"/> is reclaimed by garbage collection.
        /// </summary>
        ~XNABlendStateImplementation() {
            Dispose(false);
        }

        /// <summary>
        /// Binds the implementation. This is called the first time an unbound state is set to the device or manually by the user in order to
        /// create the underlying state ahead of time (best practice). Once called the state properties are read-only.
        /// </summary>
        public override void BindBlendState() {
            if(!base.IsBound) {
                //If blending is enabled, create it
                if(this.GetBlendEnable(0)) {
                    _blendState = new XFG.BlendState();
                    //Set alpha properties
                    _blendState.AlphaBlendFunction = XNAHelper.ToXNABlendFunction(base.AlphaBlendFunction);
                    _blendState.AlphaSourceBlend = XNAHelper.ToXNABlend(base.AlphaSourceBlend);
                    _blendState.AlphaDestinationBlend = XNAHelper.ToXNABlend(base.AlphaDestinationBlend);

                    //Set color properties
                    _blendState.ColorBlendFunction = XNAHelper.ToXNABlendFunction(base.ColorBlendFunction);
                    _blendState.ColorSourceBlend = XNAHelper.ToXNABlend(base.ColorSourceBlend);
                    _blendState.ColorDestinationBlend = XNAHelper.ToXNABlend(base.ColorDestinationBlend);

                    //Set blend factor
                    XNA.Color c;
                    Color bf = base.BlendFactor;
                    XNAHelper.ConvertColor(ref bf, out c);
                    _blendState.BlendFactor = c;

                    //Set multisample bitmask
                    _blendState.MultiSampleMask = base.MultiSampleMask;

                    //Set color write bitmasks
                    _blendState.ColorWriteChannels = (XFG.ColorWriteChannels) this.GetWriteChannels(0);
                    _blendState.ColorWriteChannels1 = (XFG.ColorWriteChannels) this.GetWriteChannels(1);
                    _blendState.ColorWriteChannels2 = (XFG.ColorWriteChannels) this.GetWriteChannels(2);
                    _blendState.ColorWriteChannels3 = (XFG.ColorWriteChannels) this.GetWriteChannels(3);
                } else {
                    //Otherwise if it is -not- enabled, just re-use the opaque state - guaranteed to never change
                    _blendState = XFG.BlendState.Opaque;
                }
                base.IsBound = true;
            }
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                //Dispose of managed resources
                if(disposing) {
                    if(_blendState != null) {
                        _blendState.Dispose();
                    }
                }
            }
            base.Dispose(disposing);
        }
    }
}
